%!TEX program = xelatex
%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode

\documentclass[12pt,t,aspectratio=169,mathserif]{beamer}
%Other possible values are: 1610, 149, 54, 43 and 32. By default, it is to 128mm by 96mm(4:3).
%run XeLaTeX to compile.

\input{wang-slides-preamble.tex}

\begin{document}

\title{第4章：NumPy 模块}
%(1.1-1.2) 
%\institute{上海立信会计金融学院}
\author{JMS LQW}
%\date{2021年3月12日}

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{6.1.1. }
\begin{frame}{目录 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{enumerate}
%\item  学会构造与操作 Numpy 的一维数组和二维数组。
%\item  学会内部和外部的输入输出的方法。
%\item  学会一些通用的函数和多项式的计算。
%\item  给定一个数组，编程计算(指数)移动平均得到的数组。
%\item  编写一个分段定义的函数。
%\item 读入一个文本格式的数据。将运算结果保存到一个文本文件。
%\item 给定一些数据，拟合一个多项式函数。
%\item 有关矩阵和线性方程组的常规计算。
\item[4.1.]  一维数组
\item[4.2.]  二维数组
\item[4.3.]  多维数组
\item[4.4.]  内部输入和输出
\item[4.5.]  外部输入和输出
\item[4.6.]  通用函数
\item[4.7.]  多项式
\item[4.8.]  线性代数
\item[4.9.]  NumPy 的更多内容，SciPy 和 SciKits
\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.0. Numpy 模块介绍}
%\begin{frame}{4.0. Numpy 模块介绍}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：什么是 Numpy 模块？它的的基本对象是什么？
\begin{python}
import numpy as np
x=np.linspace(0,1,11)
type(x)
x.<TAB>
\end{python}

\item  问：Numpy 模块的优点是什么？

\item  问：从哪里可以找到 Numpy 的参考手册？

\item  答：\url{https://numpy.org}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.0. 导入 Numpy 模块}
%\begin{frame}{4.0. 导入 Numpy 模块}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：下述两种导入 Numpy 模块的区别是什么？
\begin{python}
import numpy as np
from numpy import *
\end{python}

\item  问：如何使用 Tab 键自动代码补全功能？
\begin{python}
np.<TAB>
\end{python}

\item  问：如何查找一个函数的帮助文档？
\begin{python}
np.lookfor('cos')
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.1. 一维数组的初始构造函数}
%\begin{frame}{4.1.1. 一维数组的初始构造函数}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  如何使用 Numpy 模块构造一个一维数组？

\begin{python}
import numpy as np

xc,xd = np.linspace(0,1,11,retstep=True)
xo = np.linspace(0,1,10,endpoint=False)
xlog = np.logspace(0,100,2)

x = np.arange(1,10)

z = np.zeros(10, dtype=float)
y = np.ones(10, dtype=float)

la = np.array([1,2,3,0])
\end{python}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.2. 一维数组的相似构造函数}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  如何创建一个全零数组，其形状与给定数组一样？

\begin{python}
import numpy as np
x = np.linspace(0,1,11)
xe = np.zeros_like(x)
\end{python}

\item  问：构造一个 numpy 数组，在区间 $[0, 0.5]$ 和 $[0.6, 1]$ 中的间距为 $0.1$, 在区间 $[0.5, 0.6]$ 中的间距为 $0.01$.  
\begin{python}
import numpy as np
xL = np.linspace(0, 0.5, 5, endpoint=False)
xM = np.linspace(0.5, 0.6, 10, endpoint=False)
xR = np.linspace(0.6, 1.0, 5)
xs = np.hstack( (xL,xM,xR) )
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.3. 向量的算术运算}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：举例说明相同大小的数组之间的算术运算：
\begin{python}
import numpy as np
a = np.linspace(0,1,5)
c = np.linspace(1,3,5)
a+c, a*c, a/c
\end{python}

\item  问：一个标量与一个数组之间的算术运算是怎么进行的？
\begin{python}
import numpy as np
a = np.linspace(0,1,5)
2*a, a*2, a/5, a**3, a+2
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.3. 向量化编程的效率}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：使用三点移动平均来平滑数据。
\begin{python}
import numpy as np
f = np.array([1,7,2,5,3,6,4])
f_av = f.copy()
for i in range(1,len(f)-1):
    f_av[i] = (f[i-1]+f[i]+f[i+1])/3.0
\end{python}

\begin{python}
import numpy as np
f = np.array([1,7,2,5,3,6,4])
f_av = f.copy()
f_av[1:-1] = (f[ :-2]+f[1:-1]+f[2: ])/3.0
\end{python}

\item  问：测试大型向量数据，记录两种方法的计算时间，比较效率。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.3. 列表的切片与 Numpy 数组的切片的区别}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：列表的切片与 Numpy 数组的切片的区别是什么？
\begin{python}
a=[1,2,3,4,5]
b=a[1:-1]
b[1]=100
b
a
\end{python}

\begin{python}
import numpy as np
x=np.array([1,2,3,4,5])
y=x[1:-1]
y[1]=100
y
x
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.4. 通用函数}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：什么是通用函数？举例说明通用函数的使用方法。
\begin{python}
import numpy as np
x = np.linspace(0,1,5)
y = np.sin(x)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.4. 一个不是通用函数的例子}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：为什么下述定义的函数不是一个通用函数？
\begin{python}
import numpy as np
def h(x):
    ''' return 1 if 0<=x<=1 else 0. '''
    if x<0.0:
        return 0.0
    elif x<=1.0:
        return 1.0
    else:
        return 0.0

v = np.linspace(-2,2,41)
hv = h(v)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.5. 向量的逻辑运算符}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：对向量进行逻辑运算的结果是怎么样的？
\begin{python}
import numpy as np
x = np.linspace(-2,2,9)
y = x < 0

z = x.copy()
z[y] = -z[y]

w = x.copy()
w[w<0] = -w[w<0]

h = np.ones_like(x)
h[x<0] = 0
h[x>1] = 0
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.5. select 函数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何使用 select 函数来定义一个分段函数？
$$
k(x) = \left\{ 
\begin{array}{ll}
-x, & x<0, \\
x^3, & 0\le x<1, \\
x^2, & 1\le x<2, \\
4, & \text{otherwise}.
\end{array}
\right.
$$

\begin{python}
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-1,3,9)
choices=[ x>=2, x>=1, x>=0, x<0 ]
outcomes=[ 4.0, x**2, x**3, -x ]
k=np.select(choices,outcomes)
plt.plot(x,k,'-o')
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.5. }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{figure}
\centering
\includegraphics[height=0.6\textheight, width=0.7\textwidth]{pic/fig-4-1-5.png}
% \caption{ }
\end{figure}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.5. piecewise 函数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何使用 piecewise 函数来定义一个分段函数？
$$
m(x) = \left\{ 
\begin{array}{ll}
e^{2x}, & x<0, \\
1, & 0\le x<1, \\
e^{1-x}, & 1\le x. 
\end{array}
\right.
$$

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.5. }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容


\begin{python}
import numpy as np
import matplotlib.pyplot as plt
def m1(x):
    return np.exp(2*x)
def m2(x):
    return 1.0
def m3(x):
    return np.exp(1-x)
    
x = np.linspace(-10,10,21)
conditions = [ x>=0, x>=1, x<0 ]
functions = [ m2, m3, m1 ]
m = np.piecewise(x, conditions, functions)
plt.plot(x,m,'-o')
\end{python}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.1.5. }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{figure}
\centering
\includegraphics[height=0.6\textheight, width=0.7\textwidth]{pic/fig-4-1-5-piecewise.png}
% \caption{ }
\end{figure}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.2. 二维数组 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：一个 Numpy 数组包含三个重要的属性， ndim, shape, dtype, 它们分别是什么含义？
\begin{python}
import numpy as np
x = np.linspace(0,1,11)
x.ndim
x.shape
x.dtype
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.2.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何通过列表的列表来构建一个 Numpy 二维数组？
\begin{python}
import numpy as np
x=np.array( [ [0,1,2,3], [10,11,12,13], [20,21,22,23] ] )
x.ndim
x.shape
x.dtype
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.2.1. 广播}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：不同形状的数组之间，有时也能通过广播的方式进行算术运算，举例说明广播的规则。
\begin{python}
import numpy as np
x = np.array( [ [0,1,2,3], [10,11,12,13], [20,21,22,23] ] )
r = np.array( [2,3,4,5] )
c = np.array( [ [5], [6], [7] ] )
2*x
r*x
x*r
c*x
x*c
\end{python}

\item  问：这里的广播运算与线性代数中矩阵的乘法有什么区别？

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.2.2. 二维数组的初始构造函数}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：什么是二维数组的矩阵格式和图像格式？

\item  答：矩阵格式：从上到下、从左到右；图像格式：从左到右、从下到上。
\begin{eqnarray*}
\begin{bmatrix}
u_{00} & u_{01} & u_{02} & u_{03} \\ 
u_{10} & u_{11} & u_{12} & u_{13} \\ 
u_{20} & u_{21} & u_{22} & u_{23} \\ 
\end{bmatrix}
\hspace{1cm}
\begin{bmatrix}
u_{03} & u_{13} & u_{23}  \\ 
u_{02} & u_{12} & u_{22}  \\ 
u_{01} & u_{11} & u_{21}  \\ 
u_{00} & u_{10} & u_{20}  \\ 
\end{bmatrix}
\end{eqnarray*}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.2.2. }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何使用 \verb+np.meshgrid+ 函数构造二维数组？
\begin{python}
import numpy as np

xv = np.linspace(-1,1,5)
yv = np.linspace(-1,1,3)

[xa,ya] = np.meshgrid(xv,yv)

print('xv=\n',xv)
print('yv=\n',yv)
print('xa=\n',xa)
print('ya=\n',ya)
print('xa*ya=\n',xa*ya)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.2.2. }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何使用 \verb+np.mgrid+ 函数构造二维数组？
\begin{python}
import numpy as np

x1=np.mgrid[-1:1:9j]
x2=np.mgrid[-1:1:0.25]

[xm,ym]=np.mgrid[-1:1:5j, 0:1:3j]
print('xm=\n',xm)
print('ym=\n',ym)
print('xm*ym=\n',xm*ym)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.2.2. }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何使用 \verb+np.ogrid+ 函数构造二维数组？与 \verb+np.mgrid+ 有什么区别？
\begin{python}
import numpy as np
[xo,yo] = np.ogrid[-1:1:5j, 0:1:3j]
xo
xo.shape
yo
yo.shape
xo*yo
yo*xo
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.2.2. }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何使用 \verb+np.ones+, \verb+np.zeros+, \verb+np.empty+ 函数构造二维数组？
\begin{python}
import numpy as np
x=np.zeros((4,3), dtype=float)
x.shape
x.ndim
x.dtype
\end{python}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.2.3. 二维数组的相似构造函数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何使用 \verb+np.zeros_like+, \verb+np.ones_like+, \verb+np.empty_like+ 函数来构造二维数组？


\item  问：如何使用 \verb+np.reshape+ 函数？
\begin{python}
import numpy as np
x=range(6)
a=np.reshape(x,(2,3))
a.dtype
a.shape
a.ndim
\end{python}

\begin{python}
v=np.linspace(0,1,5)
vg=np.reshape(v,(5,1))
print('the shape of vg is',vg.shape)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.2.4. 数组的运算和通用函数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  二维数组的广播与切片是如何进行的？
\begin{python}
u=np.linspace(100,300,3)
vg=np.reshape(np.linspace(0,28,15),(5,3))
print('u=',u)
print('vg=',vg)
print('u+vg=',u+vg)
print('a slicing of vg is:',vg[1:-1,1:])
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.3. 多维数组 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  如何创建一个三维数组？
\begin{python}
[xo,yo,zo] = np.ogrid[1:2:2j,30:50:3j,600:900:4j]
print('xo=',xo)
print('yo=',yo)
print('zo=',zo)
print('xo+yo+zo=',xo+yo+zo)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.4.1. 分散的输出和输入}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何把一个数组写到一个文本文件中？
\begin{python}
import numpy as np
quarter=np.array([1,2,3,4],dtype=int)
results=np.array([37.4, 47.3, 73.4, 99])
outfile=open('q4.txt','w')
outfile.write('The results for the first four quarters\n\n')
for q,r in zip(quarter,results):
    outfile.write('For quarter %d the result is %5.1f\n'%(q,r))
outfile.close()
\end{python}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.4.1. }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何从一个包含单词和数字的文本文件中，读取数字到一个数组？
\begin{python}
infile=open('q4.txt','r')
lquarter=[]; lresult=[]
temp00=infile.readline(); temp01=infile.readline()
for line in infile:
    words=line.split()
    lquarter.append(int(words[2]))
    lresult.append(float(words[6]))
infile.close()

import numpy as np
aquarter=np.array(lquarter,dtype=int)
aresult=np.array(lresult)
print('quarters=',aquarter)
print('results=',aresult)
\end{python}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.4.2. Numpy 文本文件的输出和输入}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何将一些数据保存在一个文本文件里？
\begin{python}
import numpy as np
leng=21
x=np.linspace(0,2*np.pi,leng)
c=np.cos(x)
s=np.sin(x)
t=np.tan(x)
arr=np.empty((4,leng),dtype=float)
arr[0,: ]=x
arr[1,: ]=c
arr[2,: ]=s
arr[3,: ]=t
np.savetxt('x.txt',x)
np.savetxt('xcst.txt',(x,c,s,t))
np.savetxt('xarr.txt',arr)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.4.2. }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何从一个数据文本文件里读取数据？
\begin{python}
xc=np.loadtxt('x.txt')
xc,cc,sc,tc=np.loadtxt('xcst.txt')
arrc=np.loadtxt('xarr.txt')
\end{python}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.4.3. Numpy 二进制文件的输出和输入 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何使用二进制文件读入和保存数组？
\begin{python}
np.save('array.npy',arr)
arrd=np.load('array.npy')
\end{python}

\item  问：如何将不同形状的数组写入一个压缩的二进制文档里？如何读取？
\begin{python}
np.savez('test.npz',x=x,c=c,s=s,t=t)

temp=np.load('test.npz')
print(temp.files)
xc=temp['x']; print('xc=',xc)
cc=temp['c']
sc=temp['s']
tc=temp['t']
\end{python}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.5. 外部输入和输出}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何读入和保存 csv 文件？
\item  问：什么是 HDF 格式？
\item  问：什么是 API? 对 HDF文档，python 有哪些 API?

\item  glossary: \\
csv = comma separated values \\
HDF = Hierarchical Data Format \\
API = Application Programming Interface \\
Pandas = Python Data Analysis Library

\url{https://pandas.pydata.org}

%\vfill

%Note that installation of a suciently up-to-date version of HDF5 on Unix/Linux platforms is not for the faint-hearted.

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.6. 其它通用函数}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  举例说明下述函数的使用方法：
\begin{python}
import numpy as np
np.max
np.sum
np.cumsum
np.prod
np.cumprod
np.mean
np.median
np.average
np.var
np.std
np.cov
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.7.1. 根据数据求多项式系数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：Numpy 是如何简明地描述一个多项式的？
\item  问：如何根据数据来拟合一个多项式？
\begin{python}
import numpy as np
np.polyfit(x,y,n)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.7.2. 根据多项式求数据 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何根据一个多项式的系数，计算这个多项式的函数值？

\begin{python}
import numpy as np
x=np.linspace(0,2*np.pi,21)
y=np.sin(x)
c=np.polyfit(x,y,3)
print('c=',c)

import matplotlib.pyplot as plt
y1=np.polyval(c,x)                              <---
plt.plot(x,y,'bo-',x,y1,'r--')
plt.legend(['y=sin(x)','polynomial fit'])
plt.savefig('pic/fig-4-7-2.png')
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.7.2. }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{figure}
\centering
\includegraphics[height=0.6\textheight, width=0.7\textwidth]{pic/fig-4-7-2.png}
% \caption{ }
\end{figure}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.8. 线性代数 - 矩阵的基本运算}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何使用 np.matrix 来进行矩阵的运算？
\begin{python}
import numpy as np
A = np.matrix([[1,2],[3,4]])
b = np.matrix([[5],[6]])
A, b, A*b
\end{python}

\item  问：如何使用 np.dot 来进行矩阵的运算？
\begin{python}
import numpy as np
A = np.array([[1,2],[3,4]])
b = np.array([[5],[6]])
A, b, np.dot(A,b)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.8.1. 矩阵的基本运算 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何构造零矩阵和单位矩阵？
\begin{python}
import numpy as np
z = np.zeroes((4,4))
E = np.identity(3,dtype=float)
C = 2*np.eye(3,4,-1) + 3*np.eye(3,4,0) + 4*np.eye(3,4,1)
\end{python}

\item  问：如何使用 np.vstack 函数，将一些一维数组排成一个矩阵？
\begin{python}
v1=np.array([1,2,3])
v2=np.array([4,5,6])
rows=np.vstack((v1,v2))
print(rows)
print(rows.T)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.8.2. 矩阵的特殊运算}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：子模块 np.linalg 中有哪些对矩阵进行操作的函数？
\item  问：如何对矩阵求行列式值和逆阵？
\begin{python}
import numpy as np
import numpy.linalg as npl

A=np.array([[4,2,0],[9,3,7],[1,2,1]])
print('A=',A)
print('det(A)=',npl.det(A))
B=npl.inv(A)
print('The inverse of A is: ',B)
print('B*A=',np.dot(B,A))
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.8.2.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何求矩阵的特征值和特征向量？
\begin{python}
import numpy as np
import numpy.linalg as npl

A=np.array([[-2,-4,2],[-2,1,2],[4,2,5]])
evals,evecs = npl.eig(A)
eval1=evals[0]
evec1=evecs[:,0]
print(eval1)
print(evec1)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.8.3. 求解线性方程组}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何求解线性方程组 $Ax=b$ ?
\begin{python}
import numpy as np
import numpy.linalg as npl
A=np.array([[3,2,1],[5,5,5],[1,4,6]])
b=np.array([[5,1],[5,0],[-3,-7/2]])
x=npl.solve(A,b)
print(x)
np.dot(A,x)-b
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.9. Numpy 的更多内容}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：Numpy 的下述子模块有哪些功能？
\begin{python}
numpy.fft   # 离散傅立叶变换
numpy.random   # 生成各种分布的随机数
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.9.1. Scipy 模块 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：Scipy 模块的下述子模块有哪些功能？
\begin{python}
import numpy as np
scipy.special   # 包含许多特殊函数
scipy.integrate  # 求积分、求微分方程数值解
scipy.optimize  # 优化函数和求根
scipy.fftpack  # 更多离散傅立叶变换
scipy.linalg  # 更多线性代数
scipy.sparse  # 稀疏矩阵
scipy.sparse.linalg
\end{python}

\url{https://scipy.org}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.9.1.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  使用 Scipy 模块求解方程 $\text{coth}\, x =x$. 
\begin{python}
import numpy as np
import scipy.optimize as sco

def fun(x):
    return np.cosh(x)/np.sinh(x)-x

roots=sco.fsolve(fun,1.0)
root=roots[0]
print('The root is: %15.12f' % root)
print('The value is: %e' % fun(root))
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.9.1.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{figure}
\centering
\includegraphics[height=0.7\textheight, width=0.7\textwidth]{pic/fig-4-9-1.png}
% \caption{ }
\end{figure}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.9.1.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
import numpy as np
x=np.linspace(-3,3,21); 
y1=x; y2=np.cosh(x)/np.sinh(x)

import matplotlib.pyplot as plt
fig=plt.figure(); ax=fig.add_subplot(111)
ax.plot(x,y1,'b-',x,y2,'r-')
ax.legend(['y=x','y=coth(x)'])
ax.axis('equal')
ax.set(xlim=(-3,3),ylim=(-2,2))
ax.set(xlabel='x'); ax.set(ylabel='y')
ax.axvline(x=0); ax.axhline(y=0)
ax.set_title('Solve coth(x)=x')
fig.savefig('pic/fig-4-9-1.png')
\end{python}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.9.2. SciKits}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：介绍一下 SciKits 模块。

\item  问：介绍几种开源的协议：
\begin{itemize}
\item  BSD
\item  Apache
\item  GPL
\item  LGPL
\item  MIT
\end{itemize}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.10. }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  练习：编写程序，实现矩阵的各种分解：
\begin{itemize}
\item  LU 分解
\item  Cholesky 分解
\item  QR 分解
\item  SVD 分解
\end{itemize}

\item  练习：使用 Numpy 模块和 Scipy 模块，计算下述教材中的例题和习题：
\begin{itemize}
\item  数学分析
\item  高等代数
\item  常微分方程
\item  数值分析
\item  运筹学
\end{itemize}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.20. }
\begin{frame}{参考文献}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{thebibliography}{99}
\bibitem{stewart-en} John M. Stewart. \emph{Python for Scientists}. Second Edition. Cambridge University Press. 2017. 
\bibitem{stewart-cn} 约翰.M.斯图尔特(著). 江红.余青松(译). \emph{Python科学计算}，机械工业出版社，2019年8月第1版。

\bibitem{sauer-en} Timothy Sauer. \emph{Numerical Analysis}. Third Edition. Pearson. October 2017. 
\bibitem{sauer} Timothy Sauer(著).裴玉茹.马赓宇(译). \emph{数值分析}. 机械工业出版社. 2018年8月第1版.     
   
\end{thebibliography}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}


